<template>
    <div class="dashboard-container">
        <!-- 询价参数 -->
        <el-form ref="inquiryForm" :model="inquiryTemp" :inline="true">
            <div>
                <el-form-item label="项目名称" prop="name" required>
                    <el-input placeholder="请输入项目名称" v-model="inquiryTemp.name"></el-input>
                </el-form-item>
            </div>
            <!-- <div>
                <el-form-item label="询价类型" prop="inquiryType" required>
                    <el-radio v-model="inquiryTemp.inquiryType" :label="0">自动询价</el-radio>
                    <el-radio v-model="inquiryTemp.inquiryType" :label="1">人工询价</el-radio>
                </el-form-item>
            </div> -->
            <!-- <div>
                <el-form-item label="立刻申请" prop="submitFlag" required>
                    <el-radio v-model="inquiryTemp.submitFlag" :label="0">否</el-radio>
                    <el-radio v-model="inquiryTemp.submitFlag" :label="1">是</el-radio>
                </el-form-item>
            </div> -->
            <div v-for="(item, index) in inquiryField" :key="index">
                <el-form-item :label="item.name" :required="item.requiredFlag==1">
                    <el-input 
                        v-model="inquiryTemp.fields[index]['stringValue']" 
                        :placeholder="'请输入'+item.name" 
                        v-if="item.showType==0&&item.valueType==2"
                    ></el-input>
                    <el-input 
                        v-model="inquiryTemp.fields[index]['decimalValue']" 
                        :placeholder="'请输入'+item.name" 
                        v-if="item.showType==0&&item.valueType==0"
                    ></el-input>
                    <el-select 
                        v-model="inquiryTemp.fields[index]['enumItemName']" 
                        v-if="item.showType==3"
                    >
                        <el-option v-for="enumItem in item.relatedEnum.items" :label="enumItem.name" :value="enumItem.name" :key="enumItem.id"></el-option>
                    </el-select>
                    <div v-if="item.showType==5">
                        <el-radio v-model="inquiryTemp.fields[index]['longValue']" :label="1">是</el-radio>
                        <el-radio v-model="inquiryTemp.fields[index]['longValue']" :label="0">否</el-radio>
                    </div>
                    <el-date-picker
                        v-model="inquiryTemp.fields[index]['longValue']"
                        type="date"
                        placeholder="选择日期"
                        style="width: 100%;"
                        v-if="item.showType==2"
                        value-format="timestamp"
                    >
                    </el-date-picker>

                    <el-input 
                        v-if="inquiryTemp.showType==1"
                        type="textarea"
                        :rows="2"
                        v-model="inquiryTemp.fields[index]['stringValue']" 
                        placeholder="请输入"
                    ></el-input>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="经纬度" required>
                    {{inquiryTemp.longitude}},{{ inquiryTemp.latitude }}
                    <el-button type="primary" @click="handleSelectPoint" size="mini">选择坐标</el-button>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="业务类型" required>
                    <el-select v-model="inquiryTemp.customerCompanyBusinessEiid" placeholder="清选择业务">
                        <el-option v-for="item in customerCompanyBusinessesList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
            </div>
        </el-form>

        <!-- 土地评估对象 -->
        <el-button type="primary" size="mini" @click="handleAdd('create', 'land')">新增土地评估对象</el-button>
        <el-table :data="listLand">
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="面积(m²)" prop="area"></el-table-column>
            <el-table-column v-for="(item, index) in landField" :label="item.name" :key="item.id">
                <template slot-scope="{row}">
                    {{ item.valueType==0?row.fields[index].decimalValue:'' }}
                    {{ (item.valueType==1||item.valueType==3)?row.fields[index].longValue:'' }}
                    {{ item.valueType==2?row.fields[index].stringValue:'' }}
                    {{ item.valueType==4?row.fields[index].enumItemName:'' }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="{row, $index}">
                    <el-button type="text" style="color: red" @click="handleDeleteItem(row.id, $index, 'listLand')">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 建筑评估对象 -->
        <el-button type="primary" size="mini" @click="handleAdd('create', 'house')" style="margin-top: 10px;">新增建筑评估对象</el-button>
        <el-table :data="listHouse">
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="面积(m²)" prop="area"></el-table-column>
            <el-table-column v-for="(item, index) in houseField" :label="item.name" :key="item.id">
                <template slot-scope="{row}">
                    {{ item.valueType==0?row.fields[index].decimalValue:'' }}
                    {{ (item.valueType==1||item.valueType==3)?row.fields[index].longValue:'' }}
                    {{ item.valueType==2?row.fields[index].stringValue:'' }}
                    {{ item.valueType==4?row.fields[index].enumItemName:'' }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="{row, $index}">
                    <el-button type="text" style="color: red" @click="handleDeleteItem(row.id, $index, 'listHouse')">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 文件上传 -->
        <div style="padding-top: 20px;">
            <el-select v-model="fileTemp.categoryEiid" size="mini">
                <el-option v-for="item in fileTypeList" :label="item.label" :key="item.value" :value="item.value"></el-option>
            </el-select>
            <el-input placeholder="请输入描述" v-model="fileTemp.description" size="mini" style="width: 200px;"></el-input>
            <el-upload
                class="upload-demo"
                :action="OSS_CONFIG.host"
                :show-file-list="false"
                :before-upload="hanldeUpload"
                multiple
                :data="uploadData"
            >
                <el-button size="mini" type="primary">点击附件上传</el-button>
                <el-progress v-if="uploadProgress > 0" :percentage="uploadProgress" :show-text="false"></el-progress> <!-- 显示进度条 -->
            </el-upload>
        </div>
        <el-table :data="inquiryTemp.files" style="margin-bottom: 10px;">  
            <el-table-column label="类型" prop="">
                <template slot-scope="{row}">
                    {{ transFormTypeValue(row.categoryEiid) }}
                </template>
            </el-table-column>
            <el-table-column label="文件名" prop="showName"></el-table-column>
            <el-table-column label="描述" prop="description"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="{row, $index}">
                    <el-button type="text" style="color: red" @click="handleDeleteFile($index)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>


        <div class="submit">
            
            <el-button type="primary" @click="handleSubmit">提交</el-button>
            <el-button @click="handleBack">返回</el-button>
        </div>


        <!-- 新增、编辑 -->
        <el-dialog
            :title="dialogType=='create'?'新增':'编辑'"
            :visible="dialogVisible"
            @close="dialogVisible=false" 
        >
            <el-form ref="form" :model="temp" :rules="rules" label-width="140px">
                <el-form-item label="名称" prop="name">
                    <el-input placeholder="请输入名称" v-model="temp.name"></el-input>
                </el-form-item>
                <el-form-item label="面积(m²)" prop="area">
                    <el-input type="number" placeholder="请输入面积" v-model="temp.area "></el-input>
                </el-form-item>
                <!-- 动态字段 -->
                <el-form-item v-for="item, index in temp.fields" :key="index" :label="item.name+(item.valueUnit?`(${item.valueUnit})`:'')+':'" :required="item.requiredFlag==1">
                    <el-input 
                        v-model="temp.fields[index]['stringValue']" 
                        :placeholder="'请输入'+item.name" 
                        v-if="item.showType==0&&item.valueType==2"
                    ></el-input>
                    <el-input 
                        v-model="temp.fields[index]['decimalValue']" 
                        :placeholder="'请输入'+item.name" 
                        v-if="item.showType==0&&item.valueType==0"
                    ></el-input>
                    <el-select 
                        v-model="temp.fields[index]['enumItemName']" 
                        v-if="item.showType==3"
                    >
                        <el-option v-for="enumItem in item.relatedEnum.items" :label="enumItem.name" :value="enumItem.name" :key="enumItem.id"></el-option>
                    </el-select>
                    <div v-if="item.showType==5">
                        <el-radio v-model="temp.fields[index]['longValue']" :label="1">是</el-radio>
                        <el-radio v-model="temp.fields[index]['longValue']" :label="0">否</el-radio>
                    </div>
                    <el-date-picker
                        v-model="temp.fields[index]['longValue']"
                        type="date"
                        placeholder="选择日期"
                        style="width: 100%;"
                        v-if="item.showType==2"
                        value-format="timestamp"
                    >
                    </el-date-picker>

                    <el-input 
                        v-if="item.showType==1"
                        type="textarea"
                        :rows="2"
                        v-model="temp.fields[index]['stringValue']" 
                        placeholder="请输入"
                    ></el-input>

                </el-form-item>

                <el-form-item label="描述" prop="description">
                    <el-input type="textarea" :rows="2" placeholder="请输入描述" v-model="temp.description"></el-input>
                </el-form-item>
            </el-form>
            <div style="text-align: right;">
                <el-button type="primary" @click="submit('form')">提交</el-button>
                <el-button @click="dialogVisible = false">关闭</el-button>
            </div>
        </el-dialog>

        <!-- 选择坐标弹窗 -->
        <el-dialog 
            top="20px" 
            :visible.sync="dialogVisibleAddress" 
            :close-on-click-modal="false"
            @close="dialogVisibleAddress=false"
            :title="'当前选择坐标'+(inquiryTemp.longitude?`:(${inquiryTemp.longitude},${inquiryTemp.latitude})`:'')" 
            width="800px" class="point"
        >
            <div style="position: relative;">
                <div style="position: absolute; top: 10px; left: 10px; z-index: 9999;">
                    <el-select size="mini" v-model="cityName" placeholder="请选择城市" style="width: 100px;">
                        <el-option v-for="item in cityList" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                    <el-input placeholder="请输入地址" v-model="address" size="mini" style="width: 300px;margin-right: 5px;"></el-input>
                    <el-button type="primary" size="mini" @click="searchAddress">搜索</el-button>
                </div>
                <div id="baiduMap" style="width: 100%;height: 500px;"></div>
            </div>
            <div class="bottom" style="padding-top: 5px;">
                <el-button type="primary" @click="dialogVisibleAddress=false">确认坐标</el-button>
                <el-button @click="dialogVisibleAddress=false">取消</el-button>
            </div>
        </el-dialog>

        <!-- 等待结果 -->
        <el-dialog
            :title="inquiryStatus"
            :visible="dialogVisibleInquiry"
            width="600px"
        >
            <!-- <div style="padding: 10px 0;">{{  }}</div> -->
            <div>
                <el-button type="primary" v-if="inquiryInfo.docUrl" @click=handleDown(inquiryInfo)>下载报告</el-button>
                <el-button type="primary" @click="handleBack">返回</el-button>
            </div>
        </el-dialog>

    </div>
</template>
  
<script>
import { fieldGetList } from '@/api/field'
import { subjectCreate, subjectDelete, subjectInfo, subjectUpdate } from '@/api/subject'
import { myCompanyInfo } from '@/api/my'
import { commonGetCityList, commonGetUploadFilePolicy } from '@/api/common'
import { enumInfo } from '@/api/enum'  
import moment from 'moment'
import { inquiryCreate, inquiryInfo } from '@/api/inquiry'
import Compressor from 'compressorjs';
import { generateUUID, transfromData } from '@/utils/tools'
import axios from 'axios'
import { OSS_BASE_URL } from '@/utils/common'

let map;
let marker;
let timer; //定时任务对象

export default {
    name: 'EnquiryAdd',
    data() {
        return {
            OSS_BASE_URL,
            listLand: [],
            listHouse: [],
            landField: [],
            houseField: [],
            inquiryField: [],
            dialogType: 'create',
            objectType: '',
            dialogVisible: false,
            temp: {
                area: '',
                description: '',
                name: '',
                propertyType: '',
                fields: [{fieldId: '',longValue: '', stringValue: '', decimalValue: '', enumItemName:''}]
            },
            rules: {
                name: [{ required: true, message: '请输入项目名', trigger: 'blur' }],
                area: [{ required: true, message: '请输入面积', trigger: 'blur' }],
           },
           inquiryTemp:{
                companyTypeEiid:'',
                fields: [],
                files:[],
                inquiryType:0, //0自动 1人工
                latitude: '',
                longitude: '', 
                name: '',
                subjectIds: '',
                submitFlag: 1,
                customerUserId: 4
           },
           cityName: '', //选择的诚实
           address: '', // 填写的地址
           dialogVisibleAddress: false,
           cityList: [],
           fileTypeList: [],
           customerCompanyBusinessesList: [],
           OSS_CONFIG: {
                accessid: '',
                dir: '',
                expire: '',
                host: '',
                policy: '',
                signature: ''
            },
            fileTemp: {
                categoryEiid: '',
                description: ''
            },
            uploadData: {},
            uploadProgress: 0,
            dialogVisibleInquiry: false,
            inquiryInfo:{},
            inquiryStatus: '自动询价中....'
        }
    },
    async mounted(){
        this.getOssConfig();
        //拿到工业用地的全部字段
        let landField = await fieldGetList({ objectType: 8, inquiryFlag: 1, propertyType: 102, pageCount: 999 });
        this.landField = landField.list.filter(item=>item.templateFlag==0);
        //拿到工业房地产的全部字段
        let houseField = await fieldGetList({ objectType: 8,inquiryFlag: 1, propertyType: 2, pageCount: 999 });
        this.houseField = houseField.list.filter(item=>item.templateFlag==0);
        //拿到询价单的全部字段
        let inquiryField = await fieldGetList({ objectType: 11, pageCount: 999 });
        this.inquiryField = inquiryField.list.filter(item=>item.templateFlag==0);
        this.inquiryTemp.fields = this.inquiryField.map(item=>{
            item.fieldId = item.id;
            return item;
        })
        //获取所有的业务类型
        let business = await enumInfo({ id: 36 })
        this.customerCompanyBusinessesList = business.items.filter(item=>item.selectFlag==1).map(item=>{
            let name = item.name;
            let nowItem = JSON.parse(JSON.stringify(item));
            for(let i=1;i<item.level;i++){
            let parent =  business.items.filter(aItem=>aItem.id==nowItem.parentId);
            if(parent.length>0){
                name = parent[0].name + '-' + name
            }
            nowItem = parent;
            }
            return { label: name, value: item.id }
        })
        //获取城市列表
        let data = await commonGetCityList({ geoLevel: 2, provinceId: 11 });
        this.cityList = data.map(item=>item.name);

        //拿到文件类型
        let fileType = await enumInfo({ id: 37 })
        this.fileTypeList = fileType.items.map(item=>{
            return { label: item.name, value: item.id }
        })
        this.fileTemp.categoryEiid = this.fileTypeList[0].value  
        
    },
    methods: {
        //转化文件类型的名称
        transFormTypeValue(id){
            return transfromData(this.fileTypeList, id)
        },
        //询价等待结果
        handleInquiry(id){
            this.dialogVisibleInquiry = true;
            timer = setInterval(async ()=>{
                this.inquiryInfo = await inquiryInfo({ id })
                if(this.inquiryInfo.status==2&&this.inquiryInfo.docUrl){
                    this.inquiryStatus = '自动询价完成，请下载报告'
                    clearInterval(timer)
                }
                if(this.inquiryInfo.status==3){
                    this.inquiryStatus = this.inquiryInfo.resultMessage
                    clearInterval(timer)
                }
            },1000)
        },
        // 下载
        async handleDown(row) {
                return fetch(OSS_BASE_URL+row.docUrl).then(response=>{
                    if(!response.ok){
                        throw new Error('network not ok')
                    }
                    return response.blob();
                }).then(blob=>{
                    const url = window.URL.createObjectURL(blob);
                    const elink = document.createElement('a')
                    elink.style.display = 'none'
                    elink.href = url;
                    elink.download = row.name + '工业不动产询价单.docx'
                    document.body.appendChild(elink)
                    elink.click()
                    window.URL.revokeObjectURL(elink.href)// 释放对象
                    document.body.removeChild(elink)// 移除元素

                }).catch(error=>{
                    console.log('error', error)
                })
        },
        //点击删除文件
        handleDeleteFile(index){
            this.inquiryTemp.files.splice(index, 1)
        },
        //点击上传
        async handleDeleteItem(id, index, name){
            await subjectDelete({ ids: [id] })
            this[name].splice(index, 1)
            this.$message.success({ message: '已删除' })
        },
        uploadFile(file) {
            const formData = new FormData();
            let fileName = generateUUID() + '.' + file.name.split('.')[1];
            let url = this.OSS_CONFIG.dir + fileName;
            let { categoryEiid, description } = this.fileTemp;
            let showName = file.name;
            formData.append('name', fileName);
            formData.append('key', url);
            formData.append('policy', this.uploadData.policy);
            formData.append('OSSAccessKeyId', this.uploadData.OSSAccessKeyId);
            formData.append('success_action_status', this.uploadData.success_action_status);
            formData.append('signature', this.uploadData.signature);
            formData.append('file', file);
            // 发送POST请求到服务器，此处可以使用axios等HTTP客户端库
            axios.post(this.OSS_CONFIG.host, formData, {
                headers: { 'Content-Type': 'multipart/form-data' }
                // onUploadProgress: progressEvent => {
                //     console.log(progressEvent, 111)
                //     // 更新上传进度条的值，这里通常是百分比形式
                //     this.uploadProgress = parseInt(Math.round((progressEvent.loaded * 100) / progressEvent.total));
                //     console.log(progressEvent, 222)
                // }
            }).then(response => {
                this.uploadProgress = 0;
                this.inquiryTemp.files.push({ url, showName, description, categoryEiid })  // 处理上传成功后的逻辑
            }).catch(error => {
                console.log(error, "失败了")
                // this.handleError(error); // 处理上传失败后的逻辑
            });
        },
        //图片上传前，签名处理
        hanldeUpload(file){
            //做图片压缩处理
            const reader = new FileReader();
            reader.readAsDataURL(file); // 读取文件内容
            reader.onload = (e) => {
                new Compressor(file, {
                    quality: 1, // 压缩质量，范围0.1到1.0之间，1为原图大小，0.6为中等压缩比，可根据需要调整
                    maxWidth: 1920,
                    maxHeight: 1080,
                    convertSize: 2048,
                    success: (compressedResult) => {
                        // 使用压缩后的图片进行上传操作，此处可以直接使用compressedResult上传或者转换为FormData上传
                        this.uploadFile(compressedResult); // 调用上传方法
                    },
                    error(err) {
                        console.error('Compression failed', err.message);
                    },
                    // 其他配置...
                });
            };
            return false; // 阻止自动上传，因为我们手动处理了上传逻辑
        },
        //获取Oss详情
        async getOssConfig(){
            let data = await commonGetUploadFilePolicy();
            this.OSS_CONFIG = data;
            this.uploadData = {
                policy: data.policy,
                OSSAccessKeyId: data.accessid,
                success_action_status: '200',
                signature: data.signature,
                key: data.dir,
                name: ''
            }
        },
        //选择坐标
        handleSelectPoint(){
            this.dialogVisibleAddress = true;
            setTimeout(()=>{
                this.initMap();
            },100)
            
        },
        //初始化百度地图
        initMap(){
            map = new BMapGL.Map("baiduMap");
            var point = new BMapGL.Point(120.264267, 30.1841); // 地图中心点坐标
            map.centerAndZoom(point, 15); // 初始化地图，设置中心点坐标和地图级别
            // 打开地图缩放
            map.enableScrollWheelZoom(true);

            //监听地图点击事件
            map.addEventListener('click', (e)=>{
                map.removeOverlay(marker);
                let { lng, lat} = e.latlng;
                this.inquiryTemp.longitude = lng;
                this.inquiryTemp.latitude = lat;
                marker = new BMapGL.Marker(e.latlng);        // 创建标注   
                map.addOverlay(marker);
            })
        },
        //根据地址搜索经纬度
        searchAddress(){
            let that = this;
            var options = {
                onSearchComplete: function(results){
                    // 判断状态是否正确
                    if (local.getStatus() == BMAP_STATUS_SUCCESS){
                        that.temp.latitude = results.getPoi(0).point.lat
                        that.temp.longitude = results.getPoi(0).point.lng
                        var point = new BMapGL.Point(that.temp.longitude, that.temp.latitude); // 地图中心点坐标
                        marker = new BMapGL.Marker(point);
                        map.addOverlay(marker);
                        map.centerAndZoom(point, 15); // 初始化地图，设置中心点坐标和地图级别
                    }
                }
            };
            var local = new BMapGL.LocalSearch(map, options);
            console.log(this.cityName + this.address)
            local.search(this.cityName + this.address);
        },
        //返回
        handleBack(){
            // this.$router.history.push({ path: '/inquiry/auto' })
            this.$router.back(-1)
        },
        //提交新增
        submit(formName){
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    let temp = JSON.parse(JSON.stringify(this.temp))
                    // 判断一下自定义字段必填的内容是否都填写了 requiredFlag=1判断
                    let error = [];
                    temp.fields.forEach(item=>{
                        if(item.requiredFlag==1){
                            if((item.valueType==1||item.valueType==3)&&item.longValue==""&&item.longValue=='string')error.push(item.name)
                            if(item.valueType==0&&item.decimalValue=="")error.push(item.name)
                            if(item.valueType==2&&item.stringValue=="")error.push(item.name)
                            if(item.valueType==4&&item.enumItemName=="")error.push(item.name)
                        }
                    })
                    //增加一个特殊判断，工业用地propertyType：102， 当是标准地时，合同总价必填
                    if(temp.propertyType==102){
                        temp.fields.forEach(item=>{
                            if(item.fieldId==126&&item.longValue==1){
                                // 当是标准地时
                                let price = temp.fields.filter(item=>item.fieldId==133)[0].decimalValue;
                                if(price<=0){
                                    error.push('合同总价格')
                                }
                            }
                        })
                    }
                    if(error.length>0){
                        this.$message.error(error.join('、')+'等字段必填，否则影响评估结果')
                        return;
                    }
                    
                    temp.fields = temp.fields.map(item=>{
                        let temp = {
                            fieldId: item.fieldId,
                            showType: item.showType
                        }
                        if(item.valueType==2){
                            temp.stringValue = item.stringValue
                        }
                        if(item.valueType==0){
                            temp.decimalValue = Number(item.decimalValue)
                        }
                        if(item.valueType==1||item.valueType==3){
                            temp.longValue = Number(item.longValue)
                        }
                        if(item.valueType==4){
                            temp.enumItemName = item.enumItemName
                        }

                        return temp
                    })
                    // 开始提交
                    if(this.dialogType=='create'){
                        let id = await subjectCreate(temp)
                        temp.id = id;
                        this.$message.success({ message: '创建成功' })

                        // 转化里面的字段为可现实值;
                        temp.fields = temp.fields.map(item=>{
                            if(item.showType==2){
                                //日期控件
                                item.longValue = moment(item.longValue).format('YYYY-MM-DD')
                            }
                            if(item.showType==5){
                                item.longValue = item.longValue==1?'是':'否'
                            }
                            return item;
                        })
                        if(this.objectType=='land'){
                            this.listLand.push(temp)
                        }else if(this.objectType=='house'){
                            this.listHouse.push(temp)
                        }
                    }

                    this.dialogVisible = false;
                }else {
                    console.log('error submit!!');
                    return false;
                }
            }); 
        },
        //点击新增、编辑
        handleAdd(type, objectType){
            this.dialogType = type;
            this.objectType = objectType;
            this.dialogVisible = true;
            if(type=='create'){
                //如果是工业用地
                if(objectType=='land'){
                    let fields = this.landField.map((item)=>{
                        return { 
                            fieldId: item.id, 
                            longValue: '', stringValue: '', 
                            decimalValue: '', 
                            enumItemName:'', 
                            showType: item.showType, 
                            valueType: item.valueType,
                            name: item.name, 
                            relatedEnum: item.relatedEnum,
                            requiredFlag: item.requiredFlag,
                            valueUnit: item.valueUnit
                        }
                    })
                    this.temp = {
                        area: '',
                        description: '',
                        name: '',
                        propertyType: 102, //工业用地
                        fields
                    }
                }else if(objectType=='house'){
                    let fields = this.houseField.map((item)=>{
                        return { 
                            fieldId: item.id, 
                            longValue: '', stringValue: '', 
                            decimalValue: '', 
                            enumItemName:'', 
                            showType: item.showType, 
                            valueType: item.valueType,
                            name: item.name, 
                            relatedEnum: item.relatedEnum,
                            requiredFlag: item.requiredFlag,
                            valueUnit: item.valueUnit
                        }
                    })
                    this.temp = {
                        area: '',
                        description: '',
                        name: '',
                        propertyType: 2, //工业用地
                        fields
                    }
                }

            }else{

            }
        },
        //提交询价
        async handleSubmit(){
            let params = JSON.parse(JSON.stringify(this.inquiryTemp))
            //赋值案例对象id
            params.subjectIds = [];
            this.listLand.forEach(item=>params.subjectIds.push(item.id)) 
            this.listHouse.forEach(item=>params.subjectIds.push(item.id)) 
            //判断field必填字段是否都填了
             let error = [];
            params.fields.forEach(item=>{
                if(item.requiredFlag==1){
                    if((item.valueType==1||item.valueType==3)&&item.longValue=="")error.push(item.name)
                    if(item.valueType==0&&!item.decimalValue)error.push(item.name)
                    if(item.valueType==2&&!item.stringValue)error.push(item.name)
                    if(item.valueType==4&&!item.enumItemName)error.push(item.name)
                }
            })
            if(error.length>0){
                this.$message.error(error.join('、')+'等字段必填，否则影响评估结果')
                return;
            }
            //重新填充field字段
            params.fields = params.fields.map(item=>{
                let temp = {
                    fieldId: item.fieldId,
                }
                if(item.valueType==2){
                    temp.stringValue = item.stringValue
                }
                if(item.valueType==0){
                    temp.decimalValue = Number(item.decimalValue)
                }
                if(item.valueType==1||item.valueType==3){
                    temp.longValue = Number(item.longValue)
                }
                if(item.valueType==4){
                    temp.enumItemName = item.enumItemName
                }

                return temp
            })

            let data = await inquiryCreate(params)
            this.$message.success({ message: '提交成功' })
            if(params.inquiryType==0&&params.submitFlag==1){
                //自动询价，并且立即提交的，等待一下结果
                //请求info接口
                this.handleInquiry(data);
            }else{
                //其他情况跳转列表页面
                this.handleBack();
            }
            
            
        },
    }
}
</script>

<style lang="scss" scoped>

.dashboard {
  &-container {
    margin: 15px;
    position: relative;
    .upload-demo{
        display: inline-block;
    }
  }
}
</style>